'This whole program is based on cells as you can notice. That means that i am creating
'squares on the form, where the guys can move. So i am limiting the positions they can be in
'This has its up and downs, down side is that you cannot see your guy nicely going from one
'position to another, he looks as though he is jumping there, appearing out of nowhere. But the
'good part about making cells is that you can monitor your guys, then later on in game you
'can add some objects in the form, stones, monsters, you get to know exact positions where they are
'and you dont have to use the RECT collision detection
'Also remember, winsock cant be perfect. If you have AOL you can lag. Connection, computer...
'some packets get lost, mixed up or even joined. We will discuss this later
'The next known bug is that when you start this application in Visual Basic and someone connects to you,
'then you try to say something, the text you send he wont see! BUt you will see the text he sends.
'I wasnt able to identify why this happens, but it is like that and i cant fix it
'So what you do when you want to use SAY feature appropriatly, create an EXE of this application
'and run that EXE, the SAY feature will be flawless when you run this as EXE...
'for FOR..NEXT loops
Dim score(1) As Integer 'score(0) = you, score(1) = them
Dim i As Integer, r As Integer, c As Integer
Dim dir As String
Dim strr As String
Dim direction(9) As Integer 'direction bullet was shot at, used 9 so i can easily change # of bullets
'positions of both guys
Dim GuyXpos(2) As Integer 'should have commented this while i was programming, i think i made guyXpos(1) you and 2 them
Dim GuyYpos(2) As Integer ' look above
'if lines option is selected
Dim lines As Boolean 'to draw lines for easy shooting
'winsock stuff
Dim port As Single, hostIP As String
'winsock data arrival Dims, they will be needed later on
Dim Data As String
Dim Data2() As String
Dim data3() As String
'temporary variable for messagebox input yes/no
Dim temp As Integer
'for graphics loop in painting the picture with grass
Dim x As Integer, y As Integer
'for chat
Private namez As String
'to make sure there are no variables that we are using that are not dimmed
Option Explicit
Sub DrawLines()
'this procedure is called if the user selects the option to draw squares...
'this For..Next loop does horizontal lines
For i = 1 To 20
Form1.Line (i * 32, 0)-(i * 32, 480)
Next i
'this For..Next loop does vertical lines
For i = 1 To 15
Form1.Line (0, i * 32)-(640, i * 32)
Next i
'the form is 640 X 480 big
'this boolean holds the current state... false = no lines, true = draw lines
lines = True
End Sub
Private Sub atk_Click()
Dim num As Integer 'number of bullet thats available
Dim count As Integer 'to see if there are bullets
For i = 0 To 5
If bullet(i).Left = Guy(1).Left And bullet(i).Top = Guy(1).Top And bullet(i).Visible = False Then 'if bullet is in same spot as your tank and hasnt been shot
num = i 'get number of that bullet
count = 1 'make sure that it wont output that ur out of bullets
End If
Next i
If count = 1 Then 'if u have bullets
bullet(num).Visible = True 'the bullet that u found LAST is shot
Text1.Text = num 'for testing purposes
Timer(num).Enabled = True 'makes the timer for speed of bullet start
direction(num) = dir 'direction = way ur facing, check out keydown
Winsock1.SendData "003|" 'tells other computer u shot a bullet
Else
Chat.RemoveItem (0) 'removes first thing of textbox so that it looks like its scrolling up... same as CHAT
outofbullets = outofbullets + 1 'amount of times that this has occured
Chat.AddItem "* Out of bullets * (time #" & outofbullets & ")" 'outputs that ur out of bullets and how many times you have already tried to shoot when u had no bullets (just for their knowledge on how much of a waster they are)
End If
End Sub
Private Sub btimer_Timer(Index As Integer) 'NOTE: any word with B at begining is opponents (bad)
' this just checks their position so bullet goes right way
If bdirection(Index) = 1 Then
bb(Index).Top = bb(Index).Top - 32
ElseIf bdirection(Index) = 2 Then
bb(Index).Left = bb(Index).Left - 32
ElseIf bdirection(Index) = 3 Then
bb(Index).Top = bb(Index).Top + 32
Else
bb(Index).Left = bb(Index).Left + 32
End If
If bb(Index).Left < 0 Or bb(Index).Left > 20 * 32 Or bb(Index).Top < 0 Or bb(Index).Top > 12 * 32 Then 'if it goes off the screen then
'set its position to b on their tank and make it invisible... now its usable again
bb(Index).Visible = False
bb(Index).Left = Guy(2).Left
bb(Index).Top = Guy(2).Top
btimer(Index).Enabled = False
End If
End Sub
Private Sub Chat_Click()
Form1.SetFocus 'just incase they click on chat
End Sub
Private Sub Chat_GotFocus()
Form1.SetFocus 'incase they mess something up somehow, after all, they are bound to find a way
End Sub
Private Sub Chat_ItemCheck(Item As Integer)
Form1.SetFocus '...
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'this occurs when the user releases a key. We use this Select Case to find out which key
'the user released
rpt = 0
Select Case KeyCode
'if its key up
Case vbKeyUp
'then we first check if the Yposition of the player isnt 1, therefore he cant go more up!
If GuyYpos(1) <> 1 Then
rpt = 1
For i = 0 To 9
'moves bullets along with them
If bullet(i).Top = Guy(1).Top And bullet(i).Left = Guy(1).Left And bullet(i).Visible = False Then
bullet(i).Top = bullet(i).Top - 32
End If
Next i
'but if it is not, change his Y position one up
GuyYpos(1) = GuyYpos(1) - 1 'changes actual position of character
dir = 1 'makes direction be 1 (up), used for skins and bullet directions... same for other 3 instances
End If
Case vbKeyRight
'We check if the position of the player isnt already maximum of going right
If GuyXpos(1) <> 20 Then
rpt = 1
'moves bullets along with them
For i = 0 To 9
If bullet(i).Top = Guy(1).Top And bullet(i).Left = Guy(1).Left And bullet(i).Visible = False Then
bullet(i).Left = bullet(i).Left + 32
End If
Next i
GuyXpos(1) = GuyXpos(1) + 1
dir = 4
End If
Case vbKeyDown
'here we check if the player isnt at the bottom of the screen and he cant go any more down
If GuyYpos(1) <> 12 Then
rpt = 1
'moves bullets along with them
For i = 0 To 9
If bullet(i).Top = Guy(1).Top And bullet(i).Left = Guy(1).Left And bullet(i).Visible = False Then
bullet(i).Top = bullet(i).Top + 32
End If
Next i
GuyYpos(1) = GuyYpos(1) + 1
dir = 3
End If
Case vbKeyLeft
'if the user pressed left, but his position is already minimum left value, we cant go anymore left
If GuyXpos(1) <> 1 Then
rpt = 1
'moves bullets along with them
For i = 0 To 9
If bullet(i).Top = Guy(1).Top And bullet(i).Left = Guy(1).Left And bullet(i).Visible = False Then
atk.Enabled = False 'makes sure u dont atk @ begining before u are connected
'sets all bullets at same position as u
For i = 0 To 9
bullet(i).Top = Guy(1).Top
bullet(i).Left = Guy(1).Left
bb(i).Top = Guy(2).Top
bb(i).Left = Guy(2).Left
Next i
'asks for name to be used in chat
namez = InputBox("What is your name?")
dir = 2
'makes chatbox blank, that way when a message is sent, top item is deleted and new item is placed on bottom, this makes an actual chat effect without making the forum lose focus
Chat.AddItem ""
Chat.AddItem ""
Chat.AddItem ""
Chat.AddItem ""
Chat.AddItem ""
'to make sure all numbers generated are TRULY random
Randomize 'added later: just realized that no random numbers are used
'we set the default port
port = 5432
'we set the default position for both characters
GuyXpos(1) = 1
GuyYpos(1) = 1
GuyXpos(2) = 10
GuyYpos(2) = 10
'we move these characters to the default positions that were given above
'we use this function to paint the form1 with the picture of grass
Form1.PaintPicture picbackground(0).Picture, x, y
'increase x by 32 (one square, or cell if you like)
x = x + 32
Next c
'add 32 to Y
y = y + 32
Next r
'note: u need to wait a while if u run that in debug mode! thats 14 * 20 = 280 cycles!
'insure graphic presistance
Form1.Picture = Form1.Image
Chat.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'we want to close the winsock before we close the program
Winsock1.Close
End Sub
Private Sub gflash_Timer()
'this makes the flashing G when they are a ghost
If ghost(0).Enabled = True Then 'if they are actually a ghost
If Guy(2).Picture = ghosty Then 'if they show the G symbol
Guy(2).Picture = GuySkin(5 - dir) 'make their pic normal (later realized that this is YOUR skin... oh well, when they shoot they change to the correct direction anyways)
Else
Guy(2).Picture = ghosty
End If
End If
End Sub
Private Sub ghost_Timer(Index As Integer)
ghost(Index).Enabled = False 'after it finishes, disable this
If rempic <> 0 Then
Guy(2).Picture = GuySkin(5 - rempic).Picture
Else
Guy(2).Picture = GuySkin(4 - rempic).Picture
End If
End Sub
Private Sub mAbout_Click()
'if the user clicks About, display this message
MsgBox "This game was made by Yonatan Naamad." & vbCrLf & vbCrLf & "It is an open source two player tank game, in which you basically try to kill the other person. If you find any bugs, or if you get an error and you know what caused it, email me @ cached@gmail.com and I'll fix it and add a thank-you to this program for u :). Knock yourselves out, or should I say your opponents? :▐" & vbCrLf & vbCrLf & "Current Thank Yous: none", , "About"
End Sub
Private Sub mConnect_Click()
Timer3.Enabled = True
atk.Enabled = True
On Error Resume Next
'If the user clicks Connect, we need to get the IP of the hosting computer first !
hostIP = InputBox("Enter the host's computer name or ip address:" & vbCrLf & "(Be careful not to include any unnecessary spaces etc or error message will be generated.")
'then we connect to this IP, on the default port
Winsock1.Connect hostIP, port
'we let the user know that he is connected
'we set up the starting default positions in a case that the user had moved before clicking connect
'notice, that in mHOST sub, we do the same thing, but we do it the other way,
'we let the GuyXpos(2) = 10, GuyYpos(2) = 10 and the GuyXpos(1) = 1 and GuyYpos(1) = 1
'This is a really hard part to explain, you really need to think about it. If you click connect,
'We set up your position 10, 10 and the host will be commanding the 1,1 guy
'In Host Sub, its vice versa, the Guy(1) is given the positions in 1,1, and the Guy2 in 10,10 !